﻿// =================================================================== 
// 项目说明
//====================================================================
// 陕西龙源  @Copy Right 2006-2008
// 文件： SYS_ROLES.cs
// 项目名称：供电企业管理信息系统



// 创建时间：2010-03-19
// 负责人：
// ===================================================================
using System;
using System.Data;
using CommonLib.Model;
using CommonLib.IDAL;
using CommonLib.DALFactory;

namespace CommonLib.BLL
{
    /// <summary>
    ///   的BLL层



    /// </summary>
    public class SYS_ROLES : SYS_ROLESBLLBase
    {
		#region ----------变量定义----------
		#endregion
		
		#region ----------构造函数----------
		/// <summary>
		/// 默认构造函数



		/// </summary>
		public SYS_ROLES()
		{
		}
		#endregion
		
		#region----------函数定义---------
        public static int AddTrans(CommonLib.Model.SYS_ROLES role_models, CommonLib.Model.SYS_ROLE_FUNCTION[] fun_models)
        {
            CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
            try
            {
                myTrans.BeginTransaction();
                int row = Add(myTrans, role_models);
                if (row > 0)
                {
                    for (int i = 0; i < fun_models.Length; i++)
                    {
                        SYS_ROLE_FUNCTION.Add(myTrans, fun_models[i]);
                    }
                }
                myTrans.CommitTran();
                return row;
            }
            catch (Exception ex)
            {
                string strEx = ex.Message;
                myTrans.RollBackTran();
                return 0;
            }
        }

        public static int UpdateTrans(CommonLib.Model.SYS_ROLES role_models, CommonLib.Model.SYS_ROLE_FUNCTION[] fun_models)
        {
            CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
            try
            {
                myTrans.BeginTransaction();
                SYS_ROLE_FUNCTION.DeleteFK(myTrans, "role_id ='" + role_models.ROLE_ID + "'");
                int row = Update(myTrans, role_models);
                if (row > 0)
                {
                    for (int i = 0; i < fun_models.Length; i++)
                    {
                        SYS_ROLE_FUNCTION.Add(myTrans, fun_models[i]);
                    }
                }
                myTrans.CommitTran();
                return row;
            }
            catch (Exception ex)
            {
                string strEx = ex.Message;
                myTrans.RollBackTran();
                return 0;
            }
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns>int</returns>
        public static int DeleteTrans(string recordIDs)
        {
            CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
            try
            {
                int rows = 0;
                string[] arrRecordIDs = recordIDs.Split(';');

                myTrans.BeginTransaction();

                for (int i = 0; i < arrRecordIDs.Length-1; i++)
                {
                    string roleid = arrRecordIDs[i].ToString();
                    SYS_ROLE_FUNCTION.DeleteFK(myTrans, "role_id='" + roleid + "'");
                    SYS_OPERATOR_ROLE.DeleteFK(myTrans, "MASTER_ID in (select MASTER_ID from SYS_DEPT_ROLES where role_id='" + roleid + "')");
                    SYS_DEPT_ROLES.DeleteFK(myTrans, "role_id='" + roleid + "'");
                    rows += SYS_ROLES.Delete(myTrans, roleid);
                }

                myTrans.CommitTran();
                return rows;
            }
            catch (Exception ex)
            {
                string strEx = ex.Message;
                myTrans.RollBackTran();
                return 0;
            }
        }

        #endregion
	}
}
